<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link href="../../static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" href="../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../static/js/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../static/vui/js/datagrid.js"></script>
	<script type="text/javascript" src="../../static/js/common.js"></script>
</head>
<body>
<div id="formDiv" style="width: 100%;height: 100%;">
	<div id="table"></div>
</div>
<script>
    var params = app.getRequestParams();
	$(function () {
        app.request({
			url:'resubject/select',
			data:{},
			success:function (resp) {
                initResubjects(resp.data);
            }
		});
    });

	function initResubjects(resubjects) {
        $('#table').datagrid({
            pagination:false,
            queryParams:{relationType:params.relationType},
            columns:[
                {title:'操作',width:100,fixed:true,formatter:gridOpt},
                {field:'type',title:'收支科目',fixed:true,width:160,editor:'combobox',comboData:resubjects},
                {field:'money',title:'金额',fixed:true,width:120,editor:'decimal',n:'money'},
                {field:'remarks',title:'备注',width:180,editor:'text',length:20}
            ]
        });

        var parResubjects = parent.sesubjects;
        if(parResubjects.length > 0){
            var rows = [];
            for( var i=0 ; i<parResubjects.length ; i++ ){
                var row = parResubjects[i];
                rows[i] = {type:row.resubjectName,money:row.amount,remarks:row.remarks};
            }
            $('#table').datagrid("addRows",rows);
            var divRows = $('#table .vui-table-row');
            for( var i=0 ; i<parResubjects.length ; i++ ){
                divRows.eq(i).find(' > .vui-table-cell[field="type"]').data({value:row.resubjectId,text:row.resubjectName});
            }

        }else{
            $('#table').datagrid("addRows",[{}]);
        }

        $('input[type="radio"][value="'+params.relationType+'"]').prop('checked',true);

        function gridOpt(row){
            var addSpan = $('<span class="fix-table-icon"><i class="fa fa-plus" style="color:green;"></i></span>');

            addSpan.click(function () {
                $('#table').datagrid("addRow",{});
            });

            var delSpan = $('<span class="fix-table-icon"><i class="fa fa-remove" style="color:red;"></i></span></div>');
            delSpan.click(function () {
                var tr = $(this).parent().parent().parent();
                var len = tr.parent().find('.vui-table-row').length;
                if( len == 1 ){
                    app.message(parent.layer,'最后一行不能删除',2);
                }else{
                    tr.remove();
                }
            });

            var outDiv = $('<div></div>');
            outDiv.append(addSpan).append(delSpan);

            return outDiv;
        }

        app.getOkBtn().click(function(){
            var totalAmount = 0;
            var sesubjects = [];
            $('#table .vui-table-row').each(function () {
                var subject = $(this).find(' > .vui-table-cell[field="type"]').data();
                var money = $(this).find(' > .vui-table-cell[field="money"]').html();
                var remarks = $(this).find(' > .vui-table-cell[field="remarks"]').html();


                if( subject.value && money.length==0 ){
                    app.message(parent.layer,'账户['+subject.text+']未填写金额',2);
                    sesubjects = [];
                    return false;
                }

                if( subject.value && money.length>0 ){
                    money = money *100;
                    totalAmount = app.add(money,totalAmount);
                    sesubjects[sesubjects.length] = {resubjectId:subject.value,amount:money,resubjectName:subject.text,remarks:remarks};
                }
            });

            $('input[name="order.purPrice"]',parent.document).val(app.div(totalAmount,100));
            parent.sesubjects = sesubjects;

            app.closeLastWindow();
        });
    }
</script>
</body>
</html>